package org.secmem.takenoteclient;

import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class TakeNoteSMSReceiver extends BroadcastReceiver{
	
	public static final String TAG = "TakeNoteSMSReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		
		if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
			
			// parse SMS message
            Bundle bundle = intent.getExtras();
    		Object messages[] = (Object[])bundle.get("pdus");
    		SmsMessage smsMessage[] = new SmsMessage[messages.length];

    		int smsPieces = messages.length;
    		for(int n=0;n<smsPieces; n++)
    		{
    			smsMessage[n] = SmsMessage.createFromPdu((byte[])messages[n]);
    		}

    		Date curDate = new Date(smsMessage[0].getTimestampMillis());
    		Log.i(TAG, "SMS Timestamp : " + curDate.toString());

    		String origNumber = smsMessage[0].getOriginatingAddress();
    		String message = smsMessage[0].getMessageBody().toString();
    		Log.i(TAG, "SMS : " + origNumber + ", " + message);
			
			
			
			
		}
		
		
		
	}

}
